package ua.com.learnjava.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ExceptionController {

	@RequestMapping("/error/404")
	public String notFoundError(Model m, HttpServletRequest request) {
		//Need to be logged via Log4j
		System.out.println("######## calling unknownpage page *");
		List<String> menu = DefaultController.generateMenu(4);
		m.addAttribute("menu", menu);
		return "unknownpage";
	}

	@RequestMapping("generateTestException")
	public String generateTestException(Model m) {
		//Need to be logged via Log4j
		System.out.println("######## calling generateTestException page");
		if(true){
			throw new NullPointerException();
		}
		return "generateTestException";
	}
	
	@ExceptionHandler(Exception.class)
	@RequestMapping("/error/general")
	public ModelAndView handleException(Exception ex) {
		System.out.println("######## calling exceptionpage page");
		ModelAndView modelAndView = new ModelAndView("exceptionpage");
		modelAndView.addObject("exceptionMessage", ex);
		
		List<String> menu = DefaultController.generateMenu(4);
		modelAndView.addObject("menu", menu);
		return modelAndView;
	}

}
